package com.gzw.shop.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.gzw.shop.App;
import com.gzw.shop.Contans;
import com.gzw.shop.R;
import com.gzw.shop.adapter.BaseAdapter;
import com.gzw.shop.adapter.CardViewtemDecortion;
import com.gzw.shop.adapter.FavoritesAdapter;
import com.gzw.shop.bean.Favorites;
import com.gzw.shop.http.OkHttpHelper;
import com.gzw.shop.http.SpotsCallBack;
import com.gzw.shop.widget.GToolbar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.Response;

public class MyFavoritesActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    GToolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private FavoritesAdapter adapter;

    private OkHttpHelper helper = OkHttpHelper.getInstance();


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_favorites;
    }

    @Override
    public void initView() {

        toolbar.setIvLeftOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initData() {
        getFavorites();
    }

    private void getFavorites() {

        Long userId = App.getInstance().getUser().getId();

        Map<String, Object> params = new HashMap<>();
        params.put("user_id", userId);


        helper.get(Contans.API.FAVORITE_LIST, params, new SpotsCallBack<List<Favorites>>(this) {
            @Override
            public void onSuccess(Response response, List<Favorites> favorites) {
                showFavorites(favorites);
            }


            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void showFavorites(List<Favorites> favorites) {


        adapter = new FavoritesAdapter(this, favorites);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new CardViewtemDecortion());

        adapter.setListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void OnClick(View view, int position) {

            }
        });
    }
}
